<?php
/**
 * @date        <!-- phpDesigner :: Timestamp [08-07-2010 01:22:34] -->
 * @filesource  xfunction.login.php
 * @package     WebCms
 * @copyright	Copyright (C) 2010
 * @author      Sérgio 'Horizak' Alves
 */

// Sem acesso directo
defined('_WEXEC') or die('Restricted access');

/**
 * Login do utilizador
 */
function xajax_login($objResponse,$args = array()) {
  $where = $args[2];
  if($where == 'module') {
    $user = cms_escapeSql($args[1]['loginM']);
    $pw = cms_escapeSql($args[1]['pwM']);
    if($args[1]['rememberM']) $authExpire = 'next year';
  } elseif($where == 'component') {
    $user = cms_escapeSql($args[1]['loginC']);
    $pw = cms_escapeSql($args[1]['pwC']);
    if($args[1]['rememberC']) $authExpire = 'next year';
  }

  if(!isset($authExpire)) $authExpire = 'last year';

  xajax_verifyLogin($objResponse,$user,$pw,$authExpire);

  return $objResponse;
}

/**
 * Verificar login do utilizador
 */
function xajax_verifyLogin($objResponse,$user,$pw,$authExpire) {
  global $cms;

  if($cms->obj['user']->verify($user,$pw,$authExpire)) $objResponse->redirect(cms_linkReferer());
  else  $objResponse->alert($cms->obj['user']->error);

  return $objResponse;
}

/**
 * Logout do utilizador
 */
function xajax_logout($objResponse) {
  global $cms;

  $cms->obj['user']->logout();

  $objResponse->redirect(cms_linkReferer());
  return $objResponse;
}
?>